home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / text / faqs / python-faq.part1 < prev    next >
Encoding:
Internet Message Format  |  1994-05-05  |  27.3 KB

  1. Subject: FAQ: Python -- an object-oriented language
  2. Newsgroups: comp.lang.python,comp.answers,news.answers
  3. From: guido@cwi.nl (Guido van Rossum)
  4. Date: Wed, 4 May 1994 13:53:50 GMT
  5.  
  6. Archive-name: python-faq/part1
  7. Version: 1.7
  8. Last-modified: 4 May 1994
  9.  
  10. This article contains answers to Frequently Asked Questions about
  11. Python (an object-oriented interpreted programming language -- see
  12. the answer to question 1.1 for a short overview).
  13.  
  14. Copyright 1993, 1994 Guido van Rossum.  Unchanged electronic
  15. redistribution of this FAQ is allowed.  Printed redistribution only
  16. with permission of the author.  No warranties.
  17.  
  18. Author's address:
  19.     Guido van Rossum
  20.     CWI, dept. CST
  21.     Kruislaan 413
  22.     P.O. Box 94079
  23.     1090 GB  Amsterdam
  24.     The Netherlands
  25. Email:    guido@cwi.nl
  26.  
  27. The latest version of this FAQ is available by anonymous ftp from
  28. ftp.cwi.nl [192.16.184.180] in the directory /pub/python, with
  29. filename python-FAQ.  It will also be posted regularly to the
  30. newsgroups comp.answers and comp.lang.python.
  31.  
  32. Many FAQs, including this one, are available by anonymous ftp from
  33. rtfm.mit.edu [18.70.0.209] in the directory pub/usenet/news.answers.  
  34. The name under which a FAQ is archived appears in the Archive-name line 
  35. at the top of the article.  This FAQ is archived as python-faq/part1.
  36.  
  37. There's a mail server on that machine which will send you files from
  38. the archive by e-mail if you have no ftp access.  You send a e-mail
  39. message to mail-server@rtfm.mit.edu containing the single word help in
  40. the message body to receive instructions.
  41.  
  42. This FAQ is divided in the following chapters:
  43.  
  44.  1. General information and availability
  45.  2. Python in the real world
  46.  3. Building Python
  47.  4. Programming in Python
  48.  5. Extending Python
  49.  6. Python's design
  50.  7. Using Python on non-UNIX platforms
  51.  
  52. To find the start of a particular chapter, search for the chapter number
  53. followed by a dot and a space at the beginning of a line (e.g. to
  54. find chapter 4 in vi, type /^4\. /).
  55.  
  56. Here's an overview of the questions per chapter:
  57.  
  58.  1. General information and availability
  59.   1.1. Q. What is Python?
  60.   1.2. Q. Why is it called Python?
  61.   1.3. Q. How do I obtain a copy of the Python source?
  62.   1.4. Q. How do I get documentation on Python?
  63.   1.5. Q. Is there a newsgroup or mailing list devoted to Python?
  64.   1.6. Q. Is there a book on Python, or will there be one out soon?
  65.   1.7. Q. Are there any published articles about Python that I can quote?
  66.   1.8. Q. How does the Python version numbering scheme work?
  67.   1.9. Q. Are there other ftp sites that carry Python related material?
  68.  
  69.  2. Python in the real world
  70.   2.1. Q. How many people are using Python?
  71.   2.2. Q. Have any significant projects been done in Python?
  72.   2.3. Q. Are there any commercial projects going on using Python?
  73.   2.4. Q. How stable is Python?
  74.   2.5. Q. What new developments are expected for Python in the future?
  75.  
  76.  3. Building Python
  77.   3.1. Q. Is there a test set?
  78.   3.2. Q. When running the test set, I get complaints about floating point
  79.        operations, but when playing with floating point operations I cannot
  80.        find anything wrong with them.
  81.   3.3. Q. Link errors building Python with STDWIN on SGI IRIX.
  82.   3.4. Q. Link errors after rerunning the configure script.
  83.   3.5. Q. The python interpreter complains about options passed to a
  84.        script (after the script name).
  85.   3.6. Q. When building on the SGI, make tries to run python to create
  86.        glmodule.c, but python hasn't been built or installed yet.
  87.   3.7. Q. Other trouble building Python 1.0.2 on platform X.
  88.  
  89.  4. Programming in Python
  90.   4.1. Q. Is there a source code level debugger with breakpoints, step,
  91.        etc.?
  92.   4.2. Q. Can I create an object class with some methods implemented in
  93.        C and others in Python (e.g. through inheritance)?  (Also phrased as:
  94.        Can I use a built-in type as base class?)
  95.   4.3. Q. Is there a curses/termcap package for Python?
  96.   4.4. Q. Is there an equivalent to C's onexit() in Python?
  97.   4.5. Q. When I define a function nested inside another function, the
  98.        nested function seemingly can't access the local variables of the
  99.        outer function.  What is going on?  How do I pass local data to a
  100.        nested function?
  101.   4.6. Q. How do I iterate over a sequence in reverse order?
  102.   4.7. Q. My program is too slow.  How do I speed it up?
  103.   4.8. Q. When I have imported a module, then edit it, and import it
  104.        again (into the same Python process), the changes don't seem to take
  105.        place.  What is going on?
  106.   4.9. Q. I have a module in which I want to execute some extra code when it
  107.        is run as a script.  How do I find out whether I am running as a
  108.        script?
  109.  
  110.  5. Extending Python
  111.   5.1. Q. Can I create my own functions in C?
  112.   5.2. Q. Can I create my own functions in C++?
  113.  
  114.  6. Python's design
  115.   6.1. Q. Why isn't there a generic copying operation for objects in
  116.        Python?
  117.   6.2. Q. Why isn't there a generic way to implement persistent objects
  118.        in Python?  (Persistent == automatically saved to and restored from
  119.        disk.)
  120.   6.3. Q. Why isn't there a switch or case statement in Python?
  121.  
  122.  7. Using Python on non-UNIX platforms
  123.   7.1. Q. Is there a Mac version of Python?
  124.   7.2. Q. Is there a DOS version of Python?
  125.   7.3. Q. Is there a Windows version of Python?
  126.   7.4. Q. Is there a Windows NT version of Python?
  127.   7.5. Q. I have the DOS or Windows version but it appears to be only a
  128.        binary.  Where's the library?
  129.   7.6. Q. Where's the documentation for the Mac or PC version?
  130.   7.7. Q. The Mac (PC) version doesn't seem to have any facilities for
  131.        creating or editing programs apart from entering it interactively, and
  132.        there seems to be no way to save code that was entered interactively.
  133.        How do I create a Python program on the Mac (PC)?
  134.  
  135. To find a particular question, search for the question number followed
  136. by a dot, a space, and a Q at the beginning of a line (e.g. to find
  137. question 4.2 in vi, type /^4\.2\. Q/).
  138.  
  139.  
  140. 1. General information and availability
  141. =======================================
  142.  
  143. 1.1. Q. What is Python?
  144.  
  145. A. Python is an interpreted, interactive, object-oriented programming
  146. language.  It incorporates modules, exceptions, dynamic typing, very
  147. high level dynamic data types, and classes.  Python combines
  148. remarkable power with very clear syntax.  It has interfaces to many
  149. system calls and libraries, as well as to various window systems, and
  150. is extensible in C or C++.  It is also usable as an extension language
  151. for applications that need a programmable interface.  Finally, Python
  152. is portable: it runs on many brands of UNIX, on the Mac, and on
  153. MS-DOS.
  154.  
  155. To find out more, the best thing to do is to start reading the
  156. tutorial from the documentation set (see a few questions further
  157. down).
  158.  
  159. 1.2. Q. Why is it called Python?
  160.  
  161. A. Apart from being a computer wizard, I'm also a fan of "Monty
  162. Python's Flying Circus" (a BBC comedy series from the seventies, in
  163. the -- unlikely -- case you didn't know).  It occurred to me one day
  164. that I needed a name that was short, unique, and slightly mysterious.
  165. And I happened to be reading some scripts from the series at the
  166. time...  So then I decided to call my language Python.  But Python is
  167. not a joke.  And don't you associate it with dangerous reptiles
  168. either!
  169.  
  170. 1.3. Q. How do I obtain a copy of the Python source?
  171.  
  172. A. The latest Python source distribution is always available by
  173. anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
  174. /pub/python, with filename python<version>.tar.gz.  (Old versions may
  175. have an extension of .Z, indicating use of "compress" compression.)
  176. It is a gzip'ed tar file containing the complete C source, LaTeX
  177. documentation, Python library modules, example programs, and several
  178. useful pieces of freely distributable software.  This will compile and
  179. run out of the box on most UNIX platforms.  At the time of writing,
  180. <version> is 1.0.2.  (See section 7 for non-UNIX information.)
  181.  
  182. 1.4. Q. How do I get documentation on Python?
  183.  
  184. A. The latest Python documentation set is always available by
  185. anonymous ftp from ftp.cwi.nl [192.16.184.180] in the directory
  186. /pub/python, with filename pythondoc-ps<version>.tar.gz.  It is a
  187. gzip'ed tar file containing PostScript files of the reference manual,
  188. the library manual, and the tutorial.  At the time of writing
  189. <version> is 1.0.2.  Note that the library manual is the most
  190. important one of the set, as much of Python's power stems from the
  191. standard or built-in types, functions and modules, all of which are
  192. described here.  PostScript for a high-level description of Python is
  193. in the file nluug-paper.ps.
  194.  
  195. The following sites keep mirrors of the Python distribution:
  196.  
  197. Site            IP address     Directory
  198.  
  199. gatekeeper.dec.com    16.1.0.2    /pub/plan/python/cwi
  200. ftp.uu.net        192.48.96.9    /languages/python
  201. ftp.wustl.edu        128.252.135.4    /graphics/graphics/sgi-stuff/python
  202. ftp.funet.fi        128.214.6.100    /pub/languages/python (old?)
  203. ftp.fu-berlin.de    130.133.4.50    /pub/unix/languages/python (*python* only)
  204.  
  205. Or try archie on e.g. python1.0 to locate the nearest copy of that
  206. version...
  207.  
  208. 1.5. Q. Is there a newsgroup or mailing list devoted to Python?
  209.  
  210. A. There is a newsgroup, comp.lang.python, and a mailing list.  The
  211. newsgroup and mailing list are gatewayed into each other -- if you can
  212. read news it is not necessary to subscribe to the mailing list.  Send
  213. e-mail to python-list-request@cwi.nl to (un)subscribe to the mailing
  214. list.
  215.  
  216. 1.6. Q. Is there a book on Python, or will there be one out soon?
  217.  
  218. A. Unfortunately, not yet.  I would like to write one but my
  219. obligations at CWI include too much other work to make much progress
  220. on it.  Several parties have expressed interest in sponsoring or
  221. helping the production of a book or reference manual, but so far there
  222. are no firm plans.  If you volunteer help, by all means drop me a
  223. note!
  224.  
  225. 1.7. Q. Are there any published articles about Python that I can quote?
  226.  
  227. A. So far the only refereed and published article that describes
  228. Python in some detail is:
  229.  
  230.     Guido van Rossum and Jelke de Boer, "Interactively Testing Remote
  231.     Servers Using the Python Programming Language", CWI Quarterly, Volume
  232.     4, Issue 4 (December 1991), Amsterdam, pp 283-303.
  233.  
  234. LaTeX source for this paper is available as part of the Python source
  235. distribution.
  236.  
  237. A more recent high-level description of Python is:
  238.  
  239.     Guido van Rossum, "An Introduction to Python for UNIX/C
  240.     Programmers", in the proceedings of the NLUUG najaarsconferentie
  241.     1993 (dutch UNIX users group meeting november 1993).
  242.     
  243. PostScript for this paper and for the slides used for the accompanying
  244. presentation can be found in the ftp directory mentioned a few
  245. questions earlier, with filenames nluug-paper.ps and nluug-slides.ps,
  246. respectively.
  247.  
  248. 1.8. Q. How does the Python version numbering scheme work?
  249.  
  250. A. Python versions are numbered A.B.C.  A is the major version number
  251. -- it is only incremented for major changes in functionality or source
  252. structure.  B is the minor version number, incremented for less
  253. earth-shattering changes to a release.  C is the patchlevel -- it is
  254. incremented for each new release.  Note that in the past, patches have
  255. added significant changes; in fact the changeover from 0.9.9 to 1.0.0
  256. was the first time that either A or B changed!
  257.  
  258. 1.9. Q. Are there other ftp sites that carry Python related material?
  259.  
  260. A. An interesting ftp site for Python users is ftp.markv.com
  261. (192.122.251.1); the directory pub/python contains a growing
  262. collection of interesting Python scripts.  To submit a script for
  263. inclusion, place it together with a readme file (with extension
  264. .readme) in the publicly writable directory /incoming/python.  This
  265. service is maintained by Lance Ellinghouse <lance@markv.com>.
  266.  
  267.  
  268.  
  269. 2. Python in the real world
  270. ===========================
  271.  
  272. 2.1. Q. How many people are using Python?
  273.  
  274. A. I don't know, but the maximum number of simultaneous subscriptions
  275. to the Python mailing list before it was gatewayed into the newsgroup
  276. was about 180 (several of which were local redistribution lists).  I
  277. believe that many active Python users don't bother to subscribe to the
  278. list, and now that there's a newsgroup the mailing list subscription
  279. is even less meaningful.
  280.  
  281. 2.2. Q. Have any significant projects been done in Python?
  282.  
  283. A. Here at CWI (the home of Python), we have written a 20,000 line
  284. authoring environment for transportable hypermedia presentations, a
  285. 5,000 line multimedia teleconferencing tool, as well as many many
  286. smaller programs.
  287.  
  288. The University of Virginia uses Python to control a virtual reality
  289. engine.  Contact: Matt Conway <conway@virginia.edu>.
  290.  
  291. See also the next question.
  292.  
  293. 2.3. Q. Are there any commercial projects going on using Python?
  294.  
  295. A. Several companies have revealed to me that they are planning or
  296. considering to use Python in a future product.  The furthest is
  297. Sunrise Software, who already have a product out using Python -- they
  298. use Python for a GUI management application and an SNMP network
  299. manangement application.  Contact: <info@sunrise.com>.
  300.  
  301. Individuals at many other companies are using Python for
  302. internal development (witness their contributions to the Python
  303. mailing list or newsgroup).
  304.  
  305. Python has also been elected as an extension language by MADE, a
  306. consortium supported by the European Committee's ESPRIT program and
  307. consisting of Bull, CWI and some other European companies.  Contact:
  308. Ivan Herman <ivan@cwi.nl>.
  309.  
  310. 2.4. Q. How stable is Python?
  311.  
  312. A. Very stable.  While the current version number (1.0.2) would
  313. suggest it is in the early stages of development, in fact new, stable
  314. releases (numbered 0.9.x) have been coming out roughly every 3 to 6
  315. months for the past four years.
  316.  
  317. 2.5. Q. What new developments are expected for Python in the future?
  318.  
  319. A.  Without warranty that any of this will actually be realized: I am
  320. currently thinking about mechanisms for built-in on-line help and a
  321. switch/case statement.  There have been some discussions on
  322. hierarchical module names which might solve the problem of the
  323. development of large packages.  A pthreads interface has been
  324. contributed which I would like to merge into the latest release.  The
  325. X interface needs improving.  There are also some people
  326. (independently) working on a windowing interface based on STDWIN but
  327. with the power and ease of use of the average modern widget set.  I
  328. still hope to get some help in producing a Windows version.  It would
  329. be nice if there were a window-based class browser (Someone at CWI has
  330. contributed one using Motif but it needs some work).  Also: improved
  331. support for embedding Python in other applications, e.g. by renaming
  332. most global symbols to have a "Py" prefix and providing more
  333. documentation and threading support.
  334.  
  335.  
  336. 3. Building Python
  337. ==================
  338.  
  339. 3.1. Q. Is there a test set?
  340.  
  341. A. Yes, simply do "import testall" (or "import autotest" if you aren't
  342. interested in the output).  The standard modules whose name begins
  343. with "test" together comprise the test.  The test set doesn't test
  344. *all* features of Python but it goes a long way to confirm that a new
  345. port is actually working.  The Makefile contains an entry "make test"
  346. which runs the autotest module.
  347.  
  348. 3.2. Q. When running the test set, I get complaints about floating point
  349. operations, but when playing with floating point operations I cannot
  350. find anything wrong with them.
  351.  
  352. A. The test set makes occasional unwarranted assumptions about the
  353. semantics of C floating point operations.  Until someone donates a
  354. better floating point test set, you will have to comment out the
  355. offending floating point tests and execute similar tests manually.
  356.  
  357. 3.3. Q. Link errors building Python with STDWIN on SGI IRIX.
  358.  
  359. A. Rebuild STDWIN, specifying "CC=cc -cckr" in the Makefile.
  360.  
  361. 3.4. Q. Link errors after rerunning the configure script.
  362.  
  363. A. It is generally necessary to run "make clean" after a configuration
  364. change.
  365.  
  366. 3.5. Q. The python interpreter complains about options passed to a
  367. script (after the script name).
  368.  
  369. A. You are probably linking with GNU getopt, e.g. through -liberty.
  370. Don't.  (If you are using this because you link with -lreadline, use
  371. the readline distributed with Python instead.)
  372.  
  373. 3.6. Q. When building on the SGI, make tries to run python to create
  374. glmodule.c, but python hasn't been built or installed yet.
  375.  
  376. A. Comment out the line mentioning glmodule.c in Setup and build a
  377. python without gl first; install it or make sure it is in your $PATH,
  378. then edit the Setup file again to turn on the gl module, and make
  379. again.  You don't need to do "make clean"; you do need to run "make
  380. Makefile" in the Modules subdirectory (or just run "make" at the
  381. toplevel).
  382.  
  383. 3.7. Q. Other trouble building Python 1.0.2 on platform X.
  384.  
  385. A. Please email the details to <guido@cwi.nl> and I'll look into it.
  386.  
  387.  
  388. 4. Programming in Python
  389. ========================
  390.  
  391. 4.1. Q. Is there a source code level debugger with breakpoints, step,
  392. etc.?
  393.  
  394. A. Yes.  Check out module pdb; pdb.help() prints the documentation (or
  395. you can read it as Lib/pdb.doc).  If you use the STDWIN option,
  396. there's also a windowing interface, wdb.  You can write your own
  397. debugger by using the code for pdb or wdb as an example.
  398.  
  399. 4.2. Q. Can I create an object class with some methods implemented in
  400. C and others in Python (e.g. through inheritance)?  (Also phrased as:
  401. Can I use a built-in type as base class?)
  402.  
  403. A. No, but you can easily create a Python class which serves as a
  404. wrapper around a built-in object, e.g. (for dictionaries):
  405.  
  406.     # A user-defined class behaving almost identical
  407.     # to a built-in dictionary.
  408.     class UserDict:
  409.         def __init__(self): self.data = {}
  410.         def __repr__(self): return repr(self.data)
  411.         def __cmp__(self, dict):
  412.             if type(dict) == type(self.data):
  413.                 return cmp(self.data, dict)
  414.             else:
  415.                 return cmp(self.data, dict.data)
  416.         def __len__(self): return len(self.data)
  417.         def __getitem__(self, key): return self.data[key]
  418.         def __setitem__(self, key, item): self.data[key] = item
  419.         def __delitem__(self, key): del self.data[key]
  420.         def keys(self): return self.data.keys()
  421.         def items(self): return self.data.items()
  422.         def values(self): return self.data.values()
  423.         def has_key(self, key): return self.data.has_key(key)
  424.  
  425. 4.3. Q. Is there a curses/termcap package for Python?
  426.  
  427. A. No, but you can use the "alfa" (== character cell) version of
  428. STDWIN.  (STDWIN == Standard Windows, a portable windowing system
  429. interface by the same author, URL: ftp://ftp.cwi.nl/pub/stdwin.)
  430. This will also prepare your program for porting to windowing
  431. environments such as X11 or the Macintosh.
  432.  
  433. 4.4. Q. Is there an equivalent to C's onexit() in Python?
  434.  
  435. A. Yes, if you import sys and assign a function to sys.exitfunc, it
  436. will be called when your program exits, is killed by an unhandled
  437. exception, or (on UNIX) receives a SIGHUP or SIGTERM signal.
  438.  
  439. 4.5. Q. When I define a function nested inside another function, the
  440. nested function seemingly can't access the local variables of the
  441. outer function.  What is going on?  How do I pass local data to a
  442. nested function?
  443.  
  444. A. Python does not have arbitrarily nested scopes.  When you need to
  445. create a function that needs to access some data which you have
  446. available locally, create a new class to hold the data and return a
  447. method of an instance of that class, e.g.:
  448.  
  449.     class MultiplierClass:
  450.         def __init__(self, factor):
  451.             self.factor = factor
  452.         def multiplier(self, argument):
  453.             return argument * self.factor
  454.  
  455.     def generate_multiplier(factor):
  456.         return MultiplierClass(factor).multiplier
  457.  
  458.     twice = generate_multiplier(2)
  459.     print twice(10)
  460.     # Output: 20
  461.  
  462. 4.6. Q. How do I iterate over a sequence in reverse order?
  463.  
  464. A. If it is a list, the fastest solution is
  465.  
  466.     list.reverse()
  467.     try:
  468.         for x in list:
  469.             "do something with x"
  470.     finally:
  471.         list.reverse()
  472.  
  473. This has the disadvantage that while you are in the loop, the list
  474. is temporarily reversed.  If you don't like this, you can make a copy.
  475. This appears expensive but is actually faster than other solutions:
  476.  
  477.     rev = list[:]
  478.     rev.reverse()
  479.     for x in rev:
  480.         <do something with x>
  481.  
  482. If it isn't a list, a more general but slower solution is:
  483.  
  484.     i = len(list)
  485.     while i > 0:
  486.         i = i-1
  487.         x = list[i]
  488.         <do something with x>
  489.  
  490. A more elegant solution, is to define a class which acts as a sequence
  491. and yields the elements in reverse order (solution due to Steve
  492. Majewski):
  493.  
  494.     class Rev:
  495.         def __init__(self, seq):
  496.             self.forw = seq
  497.         def __len__(self):
  498.             return len(self.forw)
  499.         def __getitem__(self, i):
  500.             return self.forw[-(i + 1)]
  501.  
  502. You can now simply write:
  503.  
  504.     for x in Rev(list):
  505.         <do something with x>
  506.  
  507. Unfortunately, this solution is slowest of all, due the the method
  508. call overhead...
  509.  
  510. 4.7. Q. My program is too slow.  How do I speed it up?
  511.  
  512. A. That's a tough one, in general.  There are many tricks to speed up
  513. Python code; I would consider rewriting parts in C only as a last
  514. resort.  One thing to notice is that function and (especially) method
  515. calls are rather expensive; if you have designed a purely OO interface
  516. with lots of tiny functions that don't do much more than get or set an
  517. instance variable or call another method, you may consider using a
  518. more direct way, e.g. directly accessing instance variables.  Also see
  519. the standard module "profile" (described in the file
  520. "python/lib/profile.doc") which makes it possible to find out where
  521. your program is spending most of its time (if you have some patience
  522. -- the profiling itself can slow your program down by an order of
  523. magnitude).
  524.  
  525. 4.8. Q. When I have imported a module, then edit it, and import it
  526. again (into the same Python process), the changes don't seem to take
  527. place.  What is going on?
  528.  
  529. A. For efficiency reasons, Python only reads the module file on the
  530. first time a module is imported (otherwise a program consisting of
  531. many modules, each of which imports the same basic module, would read
  532. the basic module over and over again).  To force a changed module
  533. being read again, do this:
  534.  
  535.     import modname
  536.     reload(modname)
  537.  
  538. Warning: this technique is not 100% fool-proof.  In particular,
  539. modules containing statements like
  540.  
  541.     from modname import some_objects
  542.  
  543. will continue to work with the old version of the objects imported
  544. thus.
  545.  
  546. 4.9. Q. I have a module in which I want to execute some extra code when it
  547. is run as a script.  How do I find out whether I am running as a
  548. script?
  549.  
  550. A. A module can find out its own module name by alooking at the
  551. (predefined) global variable __name__.  If this has the value
  552. '__main__' you are running as a script.  E.g. if you put the following
  553. on the last line of your module, main() is called only when your
  554. module is running as a script:
  555.  
  556.     if __name__ == '__main__': main()
  557.  
  558.  
  559. 5. Extending Python
  560. ===================
  561.  
  562. 5.1. Q. Can I create my own functions in C?
  563.  
  564. A. Yes, you can create built-in modules containing functions,
  565. variables, exceptions and even new types in C.  This is explained in
  566. the document "Extending and Embedding the Python Interpreter" (the
  567. LaTeX file Doc/ext.tex).  Also read the chapter on dynamic loading.
  568.  
  569. 5.2. Q. Can I create my own functions in C++?
  570.  
  571. A. Yes, using the C-compatibility features found in C++.  Basically
  572. you place extern "C" { ... } around the Python include files and put
  573. extern "C" before each function that is going to be called by the
  574. Python interpreter.  Global or static C++ objects with constructors
  575. are probably not a good idea.
  576.  
  577.  
  578. 6. Python's design
  579. ==================
  580.  
  581. 6.1. Q. Why isn't there a generic copying operation for objects in
  582. Python?
  583.  
  584. A. Hmm.  Maybe there should be one, but it's difficult to assign a
  585. useful meaning to copying of open files, sockets and windows, or
  586. recursive data structures.  As long as you design all your classes
  587. yourself you are of course free to define a standard base class that
  588. defines an overridable copying operation for all the objects you care
  589. about.  (One practical point: it would have to be a built-in function,
  590. not a standard method name, since not all built-in object types have
  591. methods; e.g. strings, integers and tuples don't.)
  592.  
  593. 6.2. Q. Why isn't there a generic way to implement persistent objects
  594. in Python?  (Persistent == automatically saved to and restored from
  595. disk.)
  596.  
  597. A. Hmm, hmm.  Basically for the same reasons as why there is no
  598. generic copying operation.
  599.  
  600. 6.3. Q. Why isn't there a switch or case statement in Python?
  601.  
  602. A. You can do this easily enough with a sequence of
  603. if... elif... elif... else.  There have been some proposals for switch
  604. statement syntax, but there is no concensus (yet) on whether and how
  605. to do range tests.
  606.  
  607.  
  608. 7. Using Python on non-UNIX platforms
  609. =====================================
  610.  
  611. 7.1. Q. Is there a Mac version of Python?
  612.  
  613. A. Yes.  It is on most ftp sites carrying Python as python.sea.hqx --
  614. this is a self-extracting archive containing the application binary as
  615. well as the Lib modules.
  616.  
  617. 7.2. Q. Is there a DOS version of Python?
  618.  
  619. A. Yes.  More than one, actually: 16python.exe runs in standard DOS
  620. mode on 186 CPUs or higher; 32python.exe uses a DOS extender and only
  621. runs on a 386 or higher CPUs.  Although 16python.exe does not pass the
  622. test set because test_grammar is too big for the parser, it actually
  623. has about 270 kbyte of allocatable heap space, which is sufficient for
  624. fairly large programs.  32python.exe is distributed as a tar file
  625. containing the required DOS extended and 387 emulator.  Both are on
  626. most ftp sites carrying Python.
  627.  
  628. 7.3. Q. Is there a Windows version of Python?
  629.  
  630. A. Yes.  Use qwpython.exe.  The only problem with it: ^C
  631. unconditionally kills the entire program -- it does not raise
  632. KeyboardInterrupt.  You can also run 16python.exe or 32python.exe in a
  633. "DOS box", but qwpython.exe appears to be slightly faster.
  634.  
  635. 7.4. Q. Is there a Windows NT version of Python?
  636.  
  637. A. Yes.  Use ntpython.exe.  This is for Intel CPUs.  If you want a
  638. Windows user interface, use qwpython.exe.
  639.  
  640. 7.5. Q. I have the DOS or Windows version but it appears to be only a
  641. binary.  Where's the library?
  642.  
  643. A. You still need to copy the files from the distribution directory
  644. "python/Lib" to your system.  If you don't have the full distribution,
  645. you can get the file pythonlib1.0.2.tar.gz from most ftp sites carrying
  646. Python; this is a subset of the distribution containing just those
  647. file.
  648.  
  649. Once you have installed the library, you need to point sys.path to it.
  650. Assuming the library is in C:\misc\python\lib, the following commands
  651. will point your Python interpreter to it (note the doubled backslashes
  652. -- you can also use single forward slashes instead):
  653.  
  654.     >>> import sys
  655.     >>> sys.path.insert(0, 'C:\\misc\\python\\lib')
  656.     >>>
  657.  
  658. For a more permanent effect, set the environment variable PYTHONPATH,
  659. as follows (talking to a DOS prompt):
  660.  
  661.     C> SET PYTHONPATH=C:\misc\python\lib
  662.  
  663. 7.6. Q. Where's the documentation for the Mac or PC version?
  664.  
  665. A. There isn't any.  The documentation for the Unix version also
  666. applies to the Mac and PC versions.  Where applicable, differences
  667. are indicated in the text.
  668.  
  669. 7.7. Q. The Mac (PC) version doesn't seem to have any facilities for
  670. creating or editing programs apart from entering it interactively, and
  671. there seems to be no way to save code that was entered interactively.
  672. How do I create a Python program on the Mac (PC)?
  673.  
  674. A. Use an external editor.  On the Mac, I am quite happy with the Desk
  675. Accessory called Sigma Edit; this doesn't require Multifinder or
  676. System 7.  I work like this: start the interpreter; edit a module file
  677. using Sigma Edit; import and test it in the interpreter; edit again in
  678. Sigma Edit; then use the built-in function reload() to re-read the
  679. imported module; etc.
  680.  
  681. Regarding the same question for the PC, Kurt Wm. Hemr writes: "While
  682. anyone with a pulse could certainly figure out how to do the same on
  683. MS-Windows, I would recommend the NotGNU Emacs clone for MS-Windows.
  684. Not only can you easily resave and "reload()" from Python after making
  685. changes, but since WinNot auto-copies to the clipboard any text you
  686. select, you can simply select the entire procedure (function) which
  687. you changed in WinNot, switch to QWPython, and shift-ins to reenter
  688. the changed program unit."
  689.  
  690.